package org.hilo.boot.core.value;

import java.lang.reflect.Field;

import org.hilo.boot.core.FieldInitValue;
import org.hilo.boot.core.UT;

/**
 * FieldInitParser Demo： 自动设置 userId
 * 
 * @author zollty
 * @since 2018-3-26
 */
public class FieldInitParser implements FieldInitValue {
    @Override
    public boolean initField(Field field, Object standardBean) {
        String name = field.getName();
        if ("userId".equals(name)) {
            
            UT.Ref.makeAccessible(field);
            
            Object val = UT.Ref.getField(field, standardBean);
            if (val != null) {
                return false;
            }
            
            UT.Ref.setField(field, standardBean, "UserSession.getCurrentUser().getId()");
            return true;
        }
        return false;
    }
}
  
